package uk.ac.ebi.pride.spectracluster;

import com.lordjoe.algorithms.*;

/**
 * uk.ac.ebi.pride.spectracluster.SpecClusterPeak
 *    represents a peak - sorts by mz,intensity,count
 * @author Steve Lewis
 * @date 10/05/13
 */
public class SpecClusterPeak implements ISpecClusterPeak {
    public static SpecClusterPeak[] EMPTY_ARRAY = {};
    public static Class THIS_CLASS = SpecClusterPeak.class;


    private final double m_MassChargeRatio;
    private final int m_Count;
    private final double m_Intensity;


    /**
     * default count to 1
     * @param massChargeRatio
     * @param intensity
     */
    public SpecClusterPeak(double massChargeRatio, double intensity ) {
        this(massChargeRatio, intensity, 1);
    }

    /**
     *
     * @param massChargeRatio
     * @param intensity
     * @param count
     */
    public SpecClusterPeak(double massChargeRatio, double intensity, int count) {
        m_MassChargeRatio = massChargeRatio;
        m_Count = count;
        m_Intensity = intensity;
    }

    @Override
    public double getMassChargeRatio() {
        return m_MassChargeRatio;
    }

    @Override
    public double getIntensity() {
          return m_Intensity;
    }

    /**
     * number associated spectra
     *
     * @return
     */
    @Override
    public int getCount() {
         return m_Count;
    }


    @Override
    public int compareTo(ISpecClusterPeak o) {
        if(o == this)
            return 0;
        if(o.getMassChargeRatio() != getMassChargeRatio()) {
            return getMassChargeRatio() < o.getMassChargeRatio() ? -1 : 1;
        }
        if(o.getIntensity() != getIntensity()) {
             return getIntensity() < o.getIntensity() ? -1 : 1;
         }
        if(o.getCount() != getCount()) {
             return getCount() < o.getCount() ? -1 : 1;
         }

        return 0;
    }

    /**
     * like equals but weaker - says other is equivalent to this
     *  Here only MZ matters
     * @param other poiibly null other object
     * @return true if other is "similar enough to this"
     */
    @Override
    public boolean equivalent(ISpecClusterPeak other) {
        if (other == null)
            return false;
        if(Math.abs((getMassChargeRatio() - other.getMassChargeRatio())) > SMALL_MZ_DIFFERENCE)
            return false;
         // we dont care about intensity or count
         return true;
    }

    /**
     * return exactly what an MGF would use
     * @return
     */
    @Override
    public String toString() {

        String item = String.format("%10.5f",  getMassChargeRatio()).trim();
        String item2 = String.format("%8.2f",  getIntensity()).trim();
        return item + "\t" + item2;
     }
}
